//
//  BIDCommonOptionsView.m
//  cxgov
//
//  Created by mal on 16/3/6.
//  Copyright © 2016年 mal. All rights reserved.
//

#import "BIDCommonOptionsView.h"
#import "BIDOptionItemView.h"

@interface BIDCommonOptionsView()<BIDOptionItemViewDelegate>
{
    NSMutableArray *_optionItemArr;
    CGFloat _optionItemHeight;
    UIView *_maskView;
    NSArray *_optionsArr;
}
@end

@implementation BIDCommonOptionsView
@synthesize delegate;

- (id)initWithOptions:(NSArray *)options andWidth:(CGFloat)width
{
    self = [super initWithFrame:CGRectMake(0, 0, width, 157.f)];
    if(self)
    {
        _optionItemArr = [NSMutableArray array];
        _optionsArr = [[NSArray alloc] initWithArray:options];
        CGFloat titleHeight = 37;
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, titleHeight)];
        [titleLabel setBackgroundColor:[UIColor blackColor]];
        [titleLabel setFont:[UIFont systemFontOfSize:17.f]];
        [titleLabel setTextColor:[UIColor whiteColor]];
        [titleLabel setText:@" 选择常用意见"];
        [titleLabel setTextAlignment:NSTextAlignmentLeft];
        [self addSubview:titleLabel];
        for(int i=0; i<options.count; i++)
        {
            BIDOptionItemView *optionItem = [[[NSBundle mainBundle] loadNibNamed:@"BIDOptionItemView" owner:self options:nil] lastObject];
            CGRect optionItemFrame = optionItem.frame;
            _optionItemHeight = CGRectGetHeight(optionItemFrame);
            CGFloat posY = titleHeight + i*CGRectGetHeight(optionItemFrame);
            optionItem.optionText = options[i];
            optionItemFrame.size.width = width;
            optionItemFrame.origin.y = posY;
            optionItem.tag = i;
            optionItem.frame = optionItemFrame;
            optionItem.delegate = self;
            [optionItem setStatus:NO];
            [self addSubview:optionItem];
            [_optionItemArr addObject:optionItem];
        }
        CGFloat ownHeight = titleHeight + options.count * _optionItemHeight;
        CGSize screenSize = [UIScreen mainScreen].bounds.size;
        CGFloat posY = (screenSize.height - ownHeight) / 2;
        CGFloat posX = (screenSize.width - width) / 2;
        CGRect ownFrame = CGRectMake(posX, posY, width, ownHeight);
        [self setBackgroundColor:[UIColor yellowColor]];
        self.frame = ownFrame;
        //
        _maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, screenSize.height)];
        [_maskView setBackgroundColor:[UIColor colorWithWhite:0.5f alpha:0.5f]];
        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandler:)];
        [_maskView addGestureRecognizer:tapGR];
    }
    return self;
}

- (void)setOptionsWithArr:(NSArray *)options
{
}

- (void)showView
{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:_maskView];
    [keyWindow addSubview: self];
}

- (void)tapHandler:(UITapGestureRecognizer*)gr
{
    [_maskView removeFromSuperview];
    [self removeFromSuperview];
}

#pragma mark - BIDOptionItemViewDelegate
- (void)selectOptionItemAt:(NSInteger)index
{
    for(int i=0; i<_optionItemArr.count; i++)
    {
        if(i==index) continue;
        BIDOptionItemView *optionItemView = _optionItemArr[i];
        [optionItemView setStatus:NO];
    }
    [delegate getSelectCommonOption:_optionsArr[index]];
    [_maskView removeFromSuperview];
    [self removeFromSuperview];
}

@end
